package com.yimixian.app.net;

import java.util.Map;

import android.content.Context;
import android.os.AsyncTask;

import com.yimixian.app.YMXApplication;


/**
 * 网络请求工具类
 * 
 * @author
 * 
 */
public class HttpApi {
	
	public enum RequestMethod{
		METHOD_GET,
		METHOD_POST
	}
	
	/**
	 * 网络请求
	 * @param context
	 * @param method 方法名
	 * @param pairs 参数map
	 * @return
	 * @throws Exception
	 */
	public static String getString(Context context, String method, Map<String, String> pairs)
			throws Exception {
		return getString(context, method, pairs, "UTF-8", RequestMethod.METHOD_GET);
	}
	
	/**
	 * 网络请求
	 * @param context
	 * @param method 方法名
	 * @param pairs 参数map
	 * @param requestMethod 请求类型(get/post)
	 * @return
	 * @throws Exception
	 */
	public static String getString(Context context, String method, Map<String, String> pairs, RequestMethod requestMethod)
			throws Exception {
		return getString(context, method, pairs, "UTF-8", requestMethod);
	}
	
	/**
	 * 网络请求
	 * @param context
	 * @param method 方法名(可以是全路径)
	 * @param pairs 参数map
	 * @param urlEncode 请求参数编码(默认utf-8)
	 * @param requestMethod 请求类型(get/post)
	 * @return
	 * @throws Exception
	 */
	public static String getString(Context context, String method, Map<String, String> pairs, String urlEncode, RequestMethod requestMethod)
			throws Exception {
		NetManager manager = new NetManager(context);
		return manager.getContentByString(manager.HandlerRequest(method,
				pairs, urlEncode, requestMethod));
	}
	
	public static AsyncTask startGetRequest(final String method, final Map<String, String> param, final RequestCallBack callBack){
		return startRequest(method, param, RequestMethod.METHOD_GET, callBack);
	}

	public static AsyncTask startPostRequest(final String method, final Map<String, String> param, final RequestCallBack callBack){
		return startRequest(method, param, RequestMethod.METHOD_POST, callBack);
	}
	public static AsyncTask startRequest(final String method, final Map<String, String> param, final RequestMethod requestMethod, final RequestCallBack callBack){
		return new AsyncTask<Map<String, String>, Void, String>(){

			@Override
			protected String doInBackground(Map<String, String>... params) {
				try {
					return getString(YMXApplication.getInstance(), method, params[0], requestMethod);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

			@Override
			protected void onPostExecute(String result) {
				super.onPostExecute(result);
				boolean flag = false;
				if(result != null){
					flag = true;
					if(callBack != null){
						callBack.requestSuccess(result);
					}
				}
				if(!flag && callBack != null){
					callBack.requestError();
				}
				callBack.alwaysDo();
			}
		}.execute(param);
	}

	public interface RequestCallBack{
		/**
		 * 请求成功
		 * @param result 服务器返回信息
		 */
		public void requestSuccess(String result);

		/**
		 * 请求失败，服务器连接异常
		 */
		public void requestError();

		/**
		 * 不管成功失败最后都执行的方法
		 */
		public void alwaysDo();
	}

}